                              
from PySide6.QtWidgets import (QWidget,
                               QStackedLayout,
                               QLabel,
                               QApplication)
from PySide6.QtCore import Qt, QSize, Signal
from PySide6.QtGui import (QMouseEvent,
                           QImageReader,
                           QImageIOHandler,
                           QImage,
                           QTransform)
from tab1preview import Tab1widget, cv2
import numpy


class Tab1(QWidget):
    resiz = Signal(QSize)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.layout = QStackedLayout()
        self.label = QLabel(self)
        self.widget = Tab1widget()
        self.label.setText("Paste or drag in a picture here")
        self.setAcceptDrops(True)
        self.layout.addWidget(self.label)
        self.layout.addWidget(self.widget)
        self.setLayout(self.layout)
        self.widget.passurl.connect(self.mimedatahasurl)
        self.ratio = 0
        self.scr = QSize()
        self.path = ''
        self.ifrotate = False

    def enterpressed(self):
        if self.layout.currentWidget() == self.widget:
            cvimg = cv2.imdecode(numpy.fromfile(self.path, dtype=numpy.uint8), -1)
            if self.ifrotate:
                cvig = cv2.rotate(cvimg, cv2.ROTATE_90_CLOCKWISE)
                self.widget.cv2op(cvig)
            else:
                self.widget.cv2op(cvimg)
        
    def mimedatahasurl(self, url):
        name = url.split("/")[-1]
        print(name)
        self.path = url
        readerimg = QImageReader(url)
        orient = readerimg.transformation()
        pixpic = QImage(url)
        if orient == QImageIOHandler.Transformation.TransformationRotate90:
            trans90 = QTransform()
            trans90.rotate(90)
            pixpic = pixpic.transformed(trans90)
            print("rotate 90 degree")
            self.ifrotate = True
        else:
            self.ifrotate = False
        self.ratio = round(pixpic.width() / pixpic.height(), 3)
        pic = pixpic.scaled(1200, 640, Qt.KeepAspectRatio, Qt.SmoothTransformation)
        self.resiz.emit(pic.size())
        self.widget.pixpic = pic
        self.widget.update()
        if self.layout.currentWidget() == self.label:
            self.layout.setCurrentWidget(self.widget)

    def ifmimedatata(self, mimedata):
        if mimedata.hasUrls():
            url = mimedata.urls()[0].toLocalFile()
            if url != "":
                self.mimedatahasurl(url)
            else:
                print("empty path")
        else:
            print("please input a picture")

    def wheelEvent(self, event):
        angle = event.angleDelta()
        ratio = round(self.width() / self.height(), 3)
        dvlu = abs(ratio - self.ratio)
        if angle.y() > 0:
            if dvlu < 0.05:
                wid = self.width() + 50
                hei = int(50 / self.ratio) + self.height()
                sz = QSize(wid, hei)
                self.resiz.emit(sz)
            elif ratio < self.ratio:
                wid = self.width() + 50
                sz = QSize(wid, self.height())
                self.resiz.emit(sz)
            elif ratio > self.ratio:
                hei = self.height() + 50
                sz = QSize(self.width(), hei)
                self.resiz.emit(sz)
        else:
            if dvlu < 0.05:
                wid = self.width() - 50
                hei = self.height() - int(50 / self.ratio)
                sz = QSize(wid, hei)
                self.resiz.emit(sz)
            elif ratio < self.ratio:
                hei = self.height() - 50
                sz = QSize(self.width(), hei)
                self.resiz.emit(sz)
            elif ratio > self.ratio:
                wid = self.width() - 50
                sz = QSize(wid, self.height())
                self.resiz.emit(sz)

    def mouseReleaseEvent(self, event: QMouseEvent):
        if event.button() == Qt.RightButton:
            clipboard = QApplication.clipboard() 
            mimedata = clipboard.mimeData()
            self.ifmimedatata(mimedata)
    
    def dragEnterEvent(self, event):
        event.acceptProposedAction()
            
    def dropEvent(self, event):
        self.ifmimedatata(event.mimeData())
        